﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ChEngine
{
    public partial class BitBoardDriver
    {
        public enum GamePhase
        {
            Opening,Middle,End
        }

        public int GetExtimatedBoardPositionalValue(Side s,GamePhase phase)
        {
           /*
            int valueWhite = 0, valueBlack = 0;
            foreach (int cell in Unpack(WhitePawns))
            {
                if (phase == GamePhase.End)
                    valueWhite += endgamePawnSquareBonuses[63 - cell];
                else
                    if (phase == GamePhase.Opening)
                        valueWhite += openingPawnSquareBonuses[63 - cell];
                    else
                        valueWhite += pawnSquareBonuses[63 - cell];

            }
            foreach (int cell in Unpack(WhiteKing))
            {
                if (phase == GamePhase.End)
                    valueWhite += endgameKingSquareBonuses[63 - cell];
                else
                    valueWhite += kingSquareBonuses[63 - cell];
            }
            foreach (int cell in Unpack(WhiteQueen))
            {
                valueWhite += queenSquareBonuses[63 - cell];
            }
            foreach (int cell in Unpack(WhiteRooks))
            {
                valueWhite += rookSquareBonuses[63 - cell];
            }
            foreach (int cell in Unpack(WhiteKnights))
            {
                valueWhite += knightSquareBonuses[63 - cell];
            }
            foreach (int cell in Unpack(WhiteBishops))
            {
                valueWhite += bishopSquareBonuses[63 - cell];
            }
            //black
            foreach (int cell in Unpack(BlackPawns))
            {
                if (phase == GamePhase.End)
                    valueBlack += endgamePawnSquareBonuses[cell];
                else
                    if (phase == GamePhase.Opening)
                        valueBlack += openingPawnSquareBonuses[cell];
                    else
                        valueBlack += pawnSquareBonuses[cell];

            }
            foreach (int cell in Unpack(BlackKing))
            {
                if (phase == GamePhase.End)
                    valueBlack += endgameKingSquareBonuses[cell];
                else
                    valueBlack += kingSquareBonuses[cell];
            }
            foreach (int cell in Unpack(BlackQueen))
            {
                valueBlack += queenSquareBonuses[cell];
            }
            foreach (int cell in Unpack(BlackRooks))
            {
                valueBlack += rookSquareBonuses[cell];
            }
            foreach (int cell in Unpack(BlackKnights))
            {
                valueBlack += knightSquareBonuses[cell];
            }
            foreach (int cell in Unpack(BlackBishops))
            {
                valueBlack += bishopSquareBonuses[cell];
            }

            return s == Side.White ? valueWhite - valueBlack : valueBlack - valueWhite;
        }

        public int GetMoveBonus(BitMove bm, GamePhase phase)
        {
            if ( (bm.Previous & WhitePieces) != 0)
            {
                int cellFrom = 63-BitToCell(bm.Previous);
                int cellTo = 63-BitToCell(bm.Current);
                if ( (bm.Previous & WhitePawns) != 0)
                {
                    if (phase == GamePhase.End)
                        return endgamePawnSquareBonuses[cellTo] - endgamePawnSquareBonuses[cellFrom];
                    else
                        if( phase == GamePhase.Opening )
                            return openingPawnSquareBonuses[cellTo] - openingPawnSquareBonuses[cellFrom];
                            else
                            if( phase == GamePhase.Middle )
                                return pawnSquareBonuses[cellTo] - pawnSquareBonuses[cellFrom];

                }
                else
                if ((bm.Previous & WhiteKing) != 0)
                { 
                    if (phase == GamePhase.End)
                        return endgameKingSquareBonuses[cellTo] - endgameKingSquareBonuses[cellFrom];
                    else
                        return kingSquareBonuses[cellTo] - kingSquareBonuses[cellFrom];
                }
                else
                if ( (bm.Previous & WhiteQueen) != 0)
                {
                    return queenSquareBonuses[cellTo] - queenSquareBonuses[cellFrom];
                }
                else
                if ( (bm.Previous & WhiteRooks) != 0)
                {
                    return rookSquareBonuses[cellTo] - rookSquareBonuses[cellFrom];
                }
                else
                if ((bm.Previous & WhiteBishops) != 0)
                {
                    return bishopSquareBonuses[cellTo] - bishopSquareBonuses[cellFrom];
                }
                else
                if ((bm.Previous & WhiteKnights) != 0)
                {
                    return knightSquareBonuses[cellTo] - knightSquareBonuses[cellFrom];
                }
            }
            else
            if ((bm.Previous & BlackPieces) != 0)
            {
                int cellFrom = BitToCell(bm.Previous);
                int cellTo = BitToCell(bm.Current);
                if ((bm.Previous & BlackPawns) != 0)
                {
                    if (phase == GamePhase.End)
                        return endgamePawnSquareBonuses[cellTo] - endgamePawnSquareBonuses[cellFrom];
                    else
                        if (phase == GamePhase.Opening)
                            return openingPawnSquareBonuses[cellTo] - openingPawnSquareBonuses[cellFrom];
                        else
                            if (phase == GamePhase.Middle)
                                return pawnSquareBonuses[cellTo] - pawnSquareBonuses[cellFrom];

                }
                else
                    if ((bm.Previous & BlackKing) != 0)
                    {
                        if (phase == GamePhase.End)
                            return endgameKingSquareBonuses[cellTo] - endgameKingSquareBonuses[cellFrom];
                        else
                            return kingSquareBonuses[cellTo] - kingSquareBonuses[cellFrom];
                    }
                    else
                        if ((bm.Previous & BlackQueen) != 0)
                        {
                            return queenSquareBonuses[cellTo] - queenSquareBonuses[cellFrom];
                        }
                        else
                            if ((bm.Previous & BlackRooks) != 0)
                            {
                                return rookSquareBonuses[cellTo] - rookSquareBonuses[cellFrom];
                            }
                            else
                                if ((bm.Previous & BlackBishops) != 0)
                                {
                                    return bishopSquareBonuses[cellTo] - bishopSquareBonuses[cellFrom];
                                }
                                else
                                    if ((bm.Previous & BlackKnights) != 0)
                                    {
                                        return knightSquareBonuses[cellTo] - knightSquareBonuses[cellFrom];
                                    }
            }*/
            return 0;
        }

        static int[] openingPawnSquareBonuses = new int[]
        {   
          -2, -2, -2, -2, -2, -2, -2, -2, 
           2,  2, -2, -9,-10, -2,  2,  2, 
          -4, -4,  3,  6,  6,  3, -4, -4, 
          -4, -2,  4, 25, 25,  4, -2, -4, 
            0,   0,  6, 16, 16,  6,   0,   0, 
            0,   0,  6, 16, 16,  6,   0,   0, 
            0,   0,  6, 16, 16,  6,   0,   0, 
            0,   0,   0,   0,   0,   0,   0,   0  
        };

        static int[] pawnSquareBonuses = new int[]
        {   
          -2, -2,   0,   0,   0,   0, -2, -2, 
           2,  2,  2, -2, -2,  2,  2,  2, 
           2,  2,  3,  4,  4,  3,  2,  2, 
           3,  4,  4, 20, 20,  4,  4,  3, 
           3,  6, 12, 18, 18, 12,  6,  3, 
           4,  8, 14, 20, 20, 14,  8,  4, 
           5, 10, 16, 24, 24, 16, 10,  5, 
            0,   0,   0,   0,   0,   0,   0,   0  
        };

        static int[] endgamePawnSquareBonuses = new int[] 
        {   
            0,   0,   0,   0,   0,   0,   0,   0, 
            0,   0,   0,   0,   0,   0,   0,   0, 
            0,   0,   0,  2,  2,   0,   0,   0, 
           3,  8,  9, 10, 10,  9,  8,  3, 
           6, 12, 14, 18, 18, 14, 12,  6, 
           9, 16, 19, 25, 25, 19, 16,  9, 
          14, 20, 25, 30, 30, 25, 20, 14, 
            0,   0,   0,   0,   0,   0,   0,   0  
        };

        static int[] bishopSquareBonuses = new int[] 
        {
	        -12, -16, -18, -19, -19, -19, -16, -12,
	        -8, -1, -4, -5, -5, -4, -1, -8,
	        -11, -4, 4, 2, 2, 4, -4, -11,
	        -12, -6, 2, 10, 10, 2, -6, -12,
	        -12, -6, 2, 10, 10, 2, -6, -12,
	        -11, -4, 4, 2, 2, 4, -4, -11,
	        -8, -1, -4, -5, -5, -4, -1, -8,
	        -4, -8, -10, -11, -11, -10, -8, -4
        };

        static int[] knightSquareBonuses = new int[] 
        {
	        -16, 2, 22, 36, 36, 22, 2, -16,
	        -62, -44, -12, 2, 2, -12, -44, -62,
	        -48, -18, 14, 30, 30, 14, -18, -48,
	        -40, -10, 24, 40, 40, 24, -10, -40,
	        -42, -12, 22, 38, 38, 22, -12, -42,
	        -54, -24, 8, 24, 24, 8, -24, -54,
	        -72, -54, -22, -8, -8, -22, -54, -72,
	        -86, -68, -48, -34, -34, -48, -68, -86
        };

        static int[] queenSquareBonuses = new int[]
        {
	        -10, -6, -2, 1, 1, -2, -6, -10,
	        -11, -3, 2, 5, 5, 2, -3, -11,
	        -9, 0, 8, 11, 11, 8, 0, -9,
	        -8, 0, 8, 16, 16, 8, 0, -8,
	        -8, 0, 8, 16, 16, 8, 0, -8,
	        -8, 0, 8, 11, 11, 8, 0, -8,
	        -10, -2, 2, 5, 5, 2, -2, -10,
	        -13, -9, -4, -1, -1, -4, -9, -13
        };

        static int[] rookSquareBonuses = new int[]
        {
	        -13, -6, 0, 3, 3, 0, -6, -13,
	        -9, -4, 2, 5, 5, 2, -4, -9,
	        -6, 0, 4, 6, 6, 4, 0, -6,
	        -6, 0, 4, 6, 6, 4, 0, -6,
	        -6, 0, 4, 6, 6, 4, 0, -6,
	        -6, 0, 4, 7, 7, 4, 0, -6,
	        -9, -3, 3, 5, 5, 3, -3, -9,
	        -15, -7, -2, 1, 1, -2, -7, -15
        };

        static int[] kingSquareBonuses = new int[]
        {
           24, 24, 24, 16, 16,   0, 32, 32,
           24, 20, 16, 12, 12, 16, 20, 24,
           16, 12,  8,  4,  4,  8, 12, 16,
           12,  8,  4,   0,   0,  4,  8, 12,
            6,   0,   0,   0,   0,   0,   0,  6,
             0,   0,   0,   0,   0,   0,   0,   0,
             0,   0,   0,   0,   0,   0,   0,   0,
             0,   0,   0,   0,   0,   0,   0,   0
        };

        static int[] endgameKingSquareBonuses = new int[]
        {
            0,  6, 12, 18, 18, 12,  6,   0,
           6, 12, 18, 24, 24, 18, 12,  6,
          12, 18, 24, 32, 32, 24, 18, 12,
          18, 24, 32, 48, 48, 32, 24, 18,
          18, 24, 32, 48, 48, 32, 24, 18,
          12, 18, 24, 32, 32, 24, 18, 12,
           6, 12, 18, 24, 24, 18, 12,  6,
            0,  6, 12, 18, 18, 12,  6,   0
        };
    }
}
